package Messages;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Logic.LoginInfo;

import ocsf.server.ConnectionToClient;

import server.SQLConnection;

 /** this class implements the librarian manual extend book return date
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MenualExtendLending {
	private ConnectionToClient client ;
	public String bookid,title,wanted;
	static int row1,temp;
	PreparedStatement p1,p2,p6,p0;
	Date enddate;
	/**
	 * 
	 * @param client
	 */
	public MenualExtendLending(ConnectionToClient client) {
		super();
		this.client = client;
	}
/**
 * execute sql query that extend the return date of a wanted book 
 * and write the librarian name and the extend date 
 * 
 * @param str
 */
	public  void extend(String[] str){
		
		try 
		{

			//System.out.println("extending lend");
			Statement stmt;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			ResultSet rs= stmt.executeQuery("SELECT * FROM db1.lending WHERE CopyId='"+str[1]+"' AND SubscriberId='"+str[2]+"'AND Lend='"+"yes"+"';");
				try {
				
						if (rs.next())//check if subscriber lended this book
							{
								Date d=rs.getDate(5);
								p1=conn.getConnection().prepareStatement("SELECT * FROM db1.reservation WHERE BookId= ?;");
								p1.setString(1, str[1]);
								ResultSet rs1 =p1.executeQuery();
								if(!(rs1.next())){//check if book not exist in reservation
								
									PreparedStatement p3 = conn.getConnection().prepareStatement("SELECT * FROM db1.books,db1.bookscopies WHERE bookscopies.CopyId = ? AND books.ID=bookscopies.BID;");//retrive wanted status
									p3.setString(1,str[1]);
									ResultSet rs2 = p3.executeQuery();  
									rs2.next();
									wanted=rs2.getString(10);
									if(!wanted.equals("Yes")){//check for book wanted status
										java.util.Date today = new java.util.Date();
										java.sql.Date timestamp = new java.sql.Date(today.getTime());
										int day=timestamp.getDate();
										enddate=rs.getDate(5);
										//System.out.println("today:"+timestamp);	
										//System.out.println("end date:"+enddate);
										timestamp.setDate(day+7);//extend by 7 days
													
															
									if(timestamp.after(enddate)){
										//System.out.println("before");
										ResultSet rs4= stmt.executeQuery("SELECT * FROM db1.workers WHERE workerid='"+str[3]+"';");
										
										rs4.next();
										PreparedStatement ps9 = conn.getConnection().prepareStatement("update db1.lending set EndDate = ?  where `CopyId` = '"+str[1]+"'");
										 ps9.setDate(1, timestamp);
										 ps9.executeUpdate();
										 PreparedStatement ps10 = conn.getConnection().prepareStatement("update db1.lending set extenddate = ?  where `CopyId` = '"+str[1]+"'");
										 java.sql.Date timestamp33 = new java.sql.Date(today.getTime());
											 ps10.setDate(1, timestamp33);
											 ps10.executeUpdate();
										 PreparedStatement ps11 = conn.getConnection().prepareStatement("update db1.lending set extendname = ?  where `CopyId` = '"+str[1]+"'");
										
										 ps11.setString(1, rs4.getString(3));
										 ps11.executeUpdate();
										
										   client.sendToClient("MenualExtendcomplety");
									} 
										   else{
											   
												//System.out.println("not before");	
												client.sendToClient("CantExtendDateTooShort");
										   }
										  
															 
								}
									else{
										//System.out.println("Cant extend the book is wanted");	
										client.sendToClient("Cantextendthebookiswanted");
										
											}
								}
									else{
										client.sendToClient("BookAllreadyReserved");
									}
								
											}
						else 
							{
							client.sendToClient("Bookisnotlended");							
							}
  
					}
						catch (SQLException e)
								{	
								e.printStackTrace();
								}
		}
  
		catch (Exception ex )
		{
	  ex.printStackTrace();
		}
		
		
		
	}
		
		
		
	}

